package com.ghy.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ghy.pojo.Headline;
import com.ghy.pojo.vo.PortalVo;
import com.ghy.service.HeadlineService;
import com.ghy.mapper.HeadlineMapper;
import com.ghy.utils.JwtHelper;
import com.ghy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 龚毫杨
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-10-15 17:18:11
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{
    @Autowired HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;
    @Override
    public Result findAll() {
        List<Headline> headlines = headlineMapper.selectList(null);
        return Result.ok(headlines);
    }

    @Override
    public Result findNewPage(PortalVo portalVo) {
        LambdaQueryWrapper<Headline> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(portalVo.getKeyWords()),Headline::getTitle,portalVo.getKeyWords()).eq(Headline::getType,portalVo.getType());
        //分页参数
        IPage<Headline> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        //headlineMapper.selectPage(page, queryWrapper);
        //自定义方法
        headlineMapper.selectPageMap(page,portalVo);

        HashMap<String, Object> data = new HashMap<>();
        data.put("pageData",page.getRecords());
        data.put("pageNum",page.getCurrent()); //页码数
        data.put("pasgeSize",page.getSize());  //页大小
        data.put("totalPage",page.getPages()); //页总数
        data.put("totalSize",page.getTotal()); //总页数

        HashMap<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo",data);
        return Result.ok(pageInfo);

    }

    @Override
    public Result showHeadlineDetail(Integer hid) {
        //获取浏览量
        Map headlineMap = headlineMapper.queryView(hid);
        //浏览量+1
        Headline headline = new Headline();
        headline.setHid(hid);
        headline.setPageViews((Integer) headlineMap.get("pageViews") + 1);
        headlineMapper.updateById(headline);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("headline",headlineMap);

        return  Result.ok(hashMap);
    }

    @Override
    public Result insertdata(Headline headline) {
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        headline.setPageViews(0);
        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline = headlineMapper.selectById(hid);
        HashMap<String, Object> headlineMap = new HashMap<>();
        headlineMap.put("headline",headline);
        return Result.ok(headlineMap);
    }

    @Override
    public Result updateData(Headline headline) {
        //乐观锁
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        headline.setVersion(version);
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);
    }

    @Override
    public Result removeByHid(Integer hid) {
        int id = headlineMapper.deleteById(hid);
        return Result.ok(null);
    }
}




